package com.wistech.search.solr.qp;

import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QueryParsing;

public class ChineseQParser extends QParser {

	private ChineseSolrQueryParser solrQueryParser;
	private String opParam;

	public ChineseQParser(String qstr, SolrParams localParams,
			SolrParams params, SolrQueryRequest req, String op) {
		super(qstr, localParams, params, req);
		this.opParam = op;
	}

	public Query parse() throws ParseException {
		String qstr = getString();

		String defaultField = getParam(CommonParams.DF);
		if (defaultField == null) {
			defaultField = getReq().getSchema().getDefaultSearchFieldName();
		}
		solrQueryParser = new ChineseSolrQueryParser(this, defaultField);
		// these could either be checked & set here, or in the SolrQueryParser
		// constructor
		String opParam = getParam(QueryParsing.OP);
		if (opParam == null) {
			opParam = this.opParam;
		}
		if (opParam == null) {
			opParam = getReq().getSchema().getQueryParserDefaultOperator();
		}
		if (opParam != null) {
			solrQueryParser
					.setDefaultOperator("AND".equals(opParam) ? QueryParser.Operator.AND
							: QueryParser.Operator.OR);
		}
		return solrQueryParser.parse(qstr);
	}

	public String[] getDefaultHighlightFields() {
		return new String[] { solrQueryParser.getField() };
	}

}
